package com.psp.park.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.rowset.serial.SerialException;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.northend.util.NumUtil;
import com.psp.park.controller.res.BaseResult;
import com.psp.park.controller.res.ListResult;
import com.psp.park.controller.res.ObjectResult;
import com.psp.park.controller.res.bean.RAddressListBean;
import com.psp.park.controller.res.bean.RInvoiceHeaderDetailBean;
import com.psp.park.controller.res.bean.RInvoiceHeaderListBean;
import com.psp.park.controller.springmvc.req.GetInvoiceHeaderDeleteParam;
import com.psp.park.controller.springmvc.req.GetInvoiceHeaderDetailParam;
import com.psp.park.controller.springmvc.req.GetInvoiceHeaderListParam;
import com.psp.park.controller.springmvc.req.GetInvoiceHeaderSaveParam;
import com.psp.park.service.InvoiceService;
import com.psp.park.service.exception.ServiceException;
import com.psp.park.service.impl.PageResult;

@Component
public class InvoiceController {

	Logger logger = Logger.getLogger(InvoiceController.class);

	@Autowired
	InvoiceService invoiceServiceImpl;

	/**
	 * 发票头列表
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RInvoiceHeaderListBean> headerList(GetInvoiceHeaderListParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RInvoiceHeaderListBean> result = new ListResult<>();
		String token = (String) request.getAttribute("token");
		try {
			String puid = (String) request.getAttribute("userId"); // 园区业务员id
			String uid = param.getUid(); // 用户id
			int page = NumUtil.toInt(param.getPage(), 0);
			int pageSize = 10;
			PageResult<RInvoiceHeaderListBean> resData = invoiceServiceImpl.getHeaderList(page, pageSize, puid, uid);
			if (resData != null) {
				result.setTotalSize(resData.getCount());
				result.setData(resData.getData());
			}
			result.setFlag(true);
			result.setPage(page);
		} catch (ServiceException e) {
			result.setServiceException(e);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(new ServiceException("error", "发送异常"));
		}
		result.setToken(token);
		return result;
	}

	public ObjectResult<RInvoiceHeaderDetailBean> headerDetail(GetInvoiceHeaderDetailParam param,
			HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RInvoiceHeaderDetailBean> result = new ObjectResult<RInvoiceHeaderDetailBean>();
		String token = (String) request.getAttribute("token");
		try {
			String puid = (String) request.getAttribute("userId");
			String id = param.getId();
			RInvoiceHeaderDetailBean rBean = invoiceServiceImpl.getHeaderDetail(id, puid);
			result.setFlag(rBean != null);
			result.setData(rBean);
		} catch (ServiceException e) {
			result.setServiceException(e);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(new ServiceException("error", "发送异常"));
		}
		result.setToken(token);
		return result;
	}

	/**
	 * 发票头保存
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult headerSave(GetInvoiceHeaderSaveParam param, HttpServletRequest request,
			HttpServletResponse response) {
		BaseResult result = new BaseResult();
		String token = (String) request.getAttribute("token");
		try {
			String puid = (String) request.getAttribute("userId"); // 园区业务员id
			String uid = param.getUid(); // 用户id
			String id = param.getId();
			String title = param.getTitle();
			int type = NumUtil.toInt(param.getType(), 1);
			boolean flag = invoiceServiceImpl.saveHeader(id, puid, uid, title, type);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(new ServiceException("error", "发送异常"));
		}
		result.setToken(token);
		return result;
	}

	/**
	 * 发票头删除
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult headerDelete(GetInvoiceHeaderDeleteParam param, HttpServletRequest request,
			HttpServletResponse response) {
		BaseResult result = new BaseResult();
		String token = (String) request.getAttribute("token");
		try {
			String puid = (String) request.getAttribute("userId"); // 园区业务员id
			String id = param.getId();
			boolean flag = invoiceServiceImpl.deleteHeader(puid, id);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(new ServiceException("error", "发送异常"));
		}
		result.setToken(token);
		return result;
	}

}
